package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.ManufactureApplyDto;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.ManufactureApply;
import com.guigu.erp.pojo.ManufactureDesignProcedure;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.ManufactureApplyService;
import com.guigu.erp.service.ManufactureDesignProcedureService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ManufactureApplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/manufacture/apply")
@Controller
public class ManufactureApplyController {

    @Autowired
    private ManufactureApplyService manufactureApplyService;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private NumberUtils numberUtils;

    //添加生产计划
    @RequestMapping("addApply")
    @ResponseBody
    public boolean addApply(@RequestBody ManufactureApplyDto dto){
        System.out.println(dto);
        boolean flag=false;
        //拿出集合
        List<ManufactureApply> list = dto.getApplyList();
        //循环
        for (int i=0;i<list.size();i++){
            //取出一个对象
            ManufactureApply apply = list.get(i);
            //将id的值置为null
            apply.setId(null);
            //设置生产计划编号
            String typeNumber = numberUtils.getTypeNumber("300");
            apply.setApplyId(typeNumber);
            //设置登记人
            apply.setRegister(dto.getRegister());
            //设置登记时间
            apply.setRegisterTime(dto.getRegisterTime());
            //设置备注
            apply.setRemark(dto.getRemark());
            //设置标志位 未审核 未派工
            apply.setCheckTag("S001-0");
            apply.setManufactureTag("P001-0");

            //调用添加的方法
            flag=manufactureApplyService.save(apply);
        }
        return flag;
    }

    //查询未审核的生产计划表
    @RequestMapping("selectApplyNoReview")
    @ResponseBody
    public PageUtils<ManufactureApply> selectApplyNoReview(Integer pageNo,Integer pageSize){
        QueryWrapper<ManufactureApply> wrapper = new QueryWrapper<>();
        wrapper.eq("CHECK_TAG","S001-0");
        wrapper.eq("MANUFACTURE_TAG","P001-0");
        PageHelper.startPage(pageNo,pageSize);
        List<ManufactureApply> list = manufactureApplyService.list(wrapper);
        PageInfo<ManufactureApply> pageInfo = new PageInfo<>(list);
        PageUtils<ManufactureApply> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    //根据id查询生产计划
    @RequestMapping("getIdQueryApply")
    @ResponseBody
    public ManufactureApply getIdQueryApply(Integer auditId){
        ManufactureApply apply = manufactureApplyService.getById(auditId);
        return apply;
    }

    //生产计划审核
    @RequestMapping("applyReview")
    @ResponseBody
    public boolean applyReview(ManufactureApply apply){
         boolean flag = manufactureApplyService.updateById(apply);
         return flag;
    }

    //生产计划查询
    @RequestMapping("getAllApply")
    @ResponseBody
    public PageUtils<ManufactureApply> getAllApply(ManufactureApplyVo vo,Integer pageNo,Integer pageSize){
        PageUtils<ManufactureApply> apply = manufactureApplyService.getAllApply(vo, pageNo, pageSize);
        return apply;
    }

    //查询生产计划审核通过，未制定派工单
    @RequestMapping("unMakeSendWorkOrder")
    @ResponseBody
    public PageUtils<ManufactureApply> unMakeSendWorkOrder(ManufactureApplyVo vo,Integer pageNo,Integer pageSize){
        PageUtils<ManufactureApply> pageUtils = manufactureApplyService.queryUnMakeApply(vo, pageNo, pageSize);
        return pageUtils;
}

    //根据id查询生产计划
    @RequestMapping("getApply")
    @ResponseBody
    public ManufactureApply getApply(@RequestBody int[] applyId){
        ManufactureApply apply = null;
        BigDecimal num = new BigDecimal(0);
        for (int i=0;i<applyId.length;i++){
            apply=manufactureApplyService.getById(applyId[i]);
            BigDecimal temp=apply.getAmount();
            num=num.add(temp);
        }
        ManufactureApply manufactureApply = new ManufactureApply();
        manufactureApply.setRegister(apply.getRegister());
        manufactureApply.setRegisterTime(apply.getRegisterTime());
        manufactureApply.setRemark(apply.getRemark());
        manufactureApply.setProductId(apply.getProductId());
        manufactureApply.setProductName(apply.getProductName());
        manufactureApply.setAmount(num);
        return manufactureApply;
    }

    //根据产品编号查询是否做了物料/工序/工序物料设计
    @RequestMapping("judgeTheDesign")
    @ResponseBody
    public boolean judgeTheDesign(String productId){
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<>();
        QueryWrapper<ManufactureDesignProcedure> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PRODUCT_ID",productId);
        wrapper.eq("PRODUCT_ID",productId);
        DesignFile file = designFileService.getOne(wrapper);
        ManufactureDesignProcedure designProcedure = manufactureDesignProcedureService.getOne(queryWrapper);
        if(file.getDesignModuleTag().equals("W001-0")){
            return false;
        }
        if (file.getDesignProcedureTag().equals("G001-0")){
            return false;
        }
        if(designProcedure.getDesignModuleTag().equals("G002-0")){
            return false;
        }
        return true;
    }

    //根据产品编号查询生产计划表
    @RequestMapping("getProductIdQueryApply")
    @ResponseBody
    public List<ManufactureApply> getProductIdQueryApply(String pid){
        QueryWrapper<ManufactureApply> wrapper = new QueryWrapper<>();
        wrapper.eq("PRODUCT_ID",pid);
        List<ManufactureApply> list = manufactureApplyService.list(wrapper);
        return list;
    }
}
